﻿using System.Activities;
using MvcFlow.Diagnostics;
using MvcFlow.Extensions;
using MvcFlow.Mvc;

namespace MvcFlow.Activities
{
    public class HttpInteractionPointActivity : NativeActivity<HttpRequestData>
    {
        private static readonly ILog log = LogManager.GetLog("HttpInteractionPoint");

        public InArgument<IActionResult> ActionResult { get; set; }

        protected override bool CanInduceIdle
        {
            get { return true; }
        }

        protected override void Execute(NativeActivityContext context)
        {
            var actionResult    = context.GetValue(ActionResult);
            var extension       = context.GetExtension<IBookmarkOutputExtension>();

            if (extension != null)
            {
                log.Debug("Found bookmark output, pushing output value");
                extension.SetBookmarkValue("HttpRequest",actionResult);
            }

            context.CreateBookmark("HttpRequest", (innerContext, innerBookmark, value) =>
            {
                log.Debug("Resuming bookmark");
                innerContext.SetValue(Result, value as HttpRequestData);
            });
        }

        public HttpInteractionPointActivity()
        {
            DisplayName = "Http";
        }
    }
}